package dependenciesIO;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.PriorityQueue;

import customization.Constants;
import dataguide.DataGuide;
import dataguide.Node;
import dataguide.OverlappingPathsInDGs;
import dataguide.ValueNode;
import download.*;


public class XMLDocPairs {

	WebFunction F1=null;
	WebFunction F2=null;
	
	public XMLDocPairs(WebFunction F1, WebFunction F2) throws Exception {
		this.F1=F1;
		this.F2=F2;
		
	 }
	
	public HashMap<PathPair, Integer> processCalls(ArrayList<String> inputsF1, String pathInTheXLM) throws Exception{
		 HashMap<PathPair, Integer> pathPairsCount= new HashMap<PathPair, Integer>();
		 for(String callF1:inputsF1){
			    if(!callF1.contains("Madonna")) continue;
			  	String inputF1=callF1.trim().replaceAll("\\s+","+");
			  	
			  	/** we do not execute the first call; we assume that the results is already download
			  	F1.executeCall(file);**/
			  	
				DataGuide dg1=new DataGuide();
			  	dg1.makeparse(F1.getPathResultForCall(inputF1));
			  	System.out.println(dg1.toString());
			  	
			    ArrayList<String> filePaths=tryCallF2WithInputsFromValuesUnderPath(dg1,pathInTheXLM);
			    if(filePaths==null) return null;
			    processFileResultsF2(dg1, filePaths, pathPairsCount);
			    
			 }
		 return pathPairsCount;
		}
	  
	
	 public  ArrayList<String> tryCallF2WithInputsFromValuesUnderPath(DataGuide dg1, String pathInTheXLM) throws Exception{
		    ArrayList<String> filePaths= new ArrayList<>();
		 	Node parentIds=dg1.getNodeWithValuesByPathFromTheRoot.get(pathInTheXLM); 	
		  	for(ValueNode v: parentIds.getValues()){  	  		
		  		/** execute call**/
		  		String callF2=v.value.replaceAll("\\s+","+");
		  		String filePathCallF2=F2.executeCall(callF2);
		  		
		  		if(filePathCallF2==null) {
		  			System.out.println("Error: "+F2.lastDownloadErrorMessage);
		  			if(F2.lastDownloadErrorMessage.contains("Server returned HTTP response code: 400")){
		  				System.out.println("The path "+pathInTheXLM+" contains invalid parameters");
		  			}
		  		}
		  		else filePaths.add(filePathCallF2);
		  		break;
		  	}	
		  	if(filePaths.size()>0) return filePaths;
		  	else return null;
	 }
	
	  public void processFileResultsF2(DataGuide dg1, ArrayList<String> filesPathsCallF2,  HashMap<PathPair, Integer> pathPairsCount) throws Exception{
			for(String filePathCallF2:filesPathsCallF2){
				/** compute DataGuide for the result **/
				DataGuide dg2=new DataGuide();
				dg2.makeparse(filePathCallF2);
	  		
				/** we use a simple exact word comparator **/
				OverlappingPathsInDGs overlap=new OverlappingPathsInDGs(dg1, dg2, new ValueNode.SimpleCompare(), pathPairsCount);
				overlap.getOverlappingPaths();
				
				/** for the moment we only check for the first file **/
				break;
			}
	  }
	

	
	  
   public static final void showPathPairCount(HashMap<PathPair, Integer>  pathPairsCount){
	   if(pathPairsCount==null) return;
	   PriorityQueue<PathPair> orderedPaths= new PriorityQueue<PathPair>(pathPairsCount.keySet());
		  while(!orderedPaths.isEmpty()){
			  PathPair p=orderedPaths.poll();
			  System.out.println(p.pathDG1+"\t"+p.pathDG2+"\t"+pathPairsCount.get(p));
		  }
   }
	
   public static void main(String[] args) throws Exception
   {  
	  WebFunction F1= new WebFunction("getArtistByName", "musicbrainz", "http://musicbrainz.org/ws/2/artist/?query=", "");
	  WebFunction F2= new WebFunction("getReleasesByArtistId", "musicbrainz", "http://musicbrainz.org/ws/2/artist/", "?inc=releases");
	  
	  XMLDocPairs dgs= new XMLDocPairs(F1, F2);
	  ArrayList<String> inputsF1=WebFunction.readInputsFromInputsAndEntitiesFiles(Constants.fileWithInputsAndEntities, 1);
	  
	  /** compute the overlap for the case where the inputs are the ids */
	  HashMap<PathPair, Integer>  pathPairsCount=dgs.processCalls(inputsF1, "metadata/artist-list/artist/id/");
	  showPathPairCount(pathPairsCount);
	  
	  System.out.println("\n\n\n\n\n\n With the inputs ipi-lisp/ipi");
	  HashMap<PathPair, Integer>  pathPairsCount2=dgs.processCalls(inputsF1, "metadata/artist-list/artist/ipi-list/ipi/");
	  showPathPairCount(pathPairsCount2); 
   }
	
   
	 
}


